<?php

function test_helpers(): string
{
    return "test_helpers";
}

function result($status, $message, $data = []): \Illuminate\Http\JsonResponse
{
    $arr = [
        'status' => $status,
        'message'=> $message,
        'data'   => $data
    ];
    return response()->json($arr);
}


/**
 * curl get 请求
 * @param string $url get请求地址
 * @param int $httpCode 返回状态码
 * @return bool|string
 */
function curl_get(string $url, &$httpCode = 0): bool|string
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //不做证书校验，在 Linux环境下请改为 true
    //curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_contents = curl_exec($ch);
    $httpCode      = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

/**
 * curl post 请求
 * @param $url
 * @param $rawData
 * @return bool|string
 */
function curl_post_raw($url, $rawData): bool|string
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: text'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

/**
 * 获取随机字符串
 * @param $length
 * @return null|string
 */
function getRandChar($length)
{
    $str    = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max    = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];
    }
    return $str;
}

/**
 * 将下划线字符串转换为(小/大)驼峰
 * @param string $str
 * @param false $capitalized 是否大写
 * @return array|string
 */
function snakeToCamel(string $str, bool $capitalized = false): array|string
{
    $ret = str_replace('_', '', ucwords($str, '_'));
    if (!$capitalized) {
        $ret = lcfirst($ret); // 转换成小驼峰
    }
    return $ret;
}
